#include "debug.h"
#include "Demura.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc.h"
#include "fsmc.h"

/********************************************************************************************
Write_Demura_Data_to_RAM函数作为补偿资料载入Demura RAM的统一入口   
u8 buf2PC[10];
DemuraIctype->IC型号; Demura_OLEDModule_flag->项目号	
编号同上位机Demura通用界面下的编号顺序相对应
编号0 ->  R66455_GM820(639)
编号1 ->  R66456_GM825(639) 
(详细查询上位机Demura界面Project集合)

Demura_OLEDModule_flag=USB_Rx_Buffer[1]	; 
********************************************************************************************/
void Write_Data_to_Demura_RAM(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{			
		switch(Demura_OLEDModule_flag)
		{
			case R66455_GM820:  //编号：0
				R66455_GM820_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;		
			case R66456_GM825:  //编号：1
				R66456_GM825_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;		
			case RM692A6_GM830: //编号：2
				RM692A6_GM830_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;	
			case RM692A9_GM870: //编号：3
				RM692A9_GM870_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;				
			case RM69298_GM760: //编号：6
				RM69298_GM760_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;		
			case RM69350_GM620: //编号：7
				RM69350_GM620_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;		
			case NT37700C_GM820://编号：8
				NT37700C_GM820_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;	
			case R66451_ALPHA:	//编号：15
				R66451_ALPHA_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case SD5202_GM583:  //编号：16
				SD5202_GM583_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case RM69351_GM920: //编号：17		
				RM69351_GM920_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case R66451_GM862:	//编号：18
				R66451_GM862_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case RM692C9_GM828: //编号：20
				RM692C9_GM828_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case RM692C9_GM930: //编号：24
				RM692C9_GM930_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case RM692D7_GM920:	//编号：25
				RM692D7_GM920_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case NT37800A_GM862://编号：26
				NT37800A_GM862_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case NT37701A_GM040://编号：27
				NT37701A_GM040_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case RM692EX_DemuraIP://编号：28
				RM692EX_DemuraIP_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case RM692D9_GM930: //编号：29
			  RM692Dx_GM930_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
		  case VT6110C_601:   //编号：30
				VT6110C_601_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;	
			case IL7835_664:    //编号：31
				IL7835_664_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;	
			case NT37701_GM820: //编号：32
				NT37701_GM820_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;	
			case RM692E1_667:  
				RM692E1_667_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;	
			case RM692D0_667:
				RM692Dx_GM051_667_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;	
			case RM692C9_GM080: //编号：36
				RM692C9_GM080_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;	
			case NT37800A_GM040://编号：37
				NT37800A_GM040_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case RM692C9_GM060://编号：40
				RM692C9_GM060_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case RM692C9_GM070://编号：41
				RM692C9_GM070_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case VTD6130_XXXXX:  //编号：43
				VTD6130_XXXXX_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case VTD6110C_GM090:
				VTD6110C_GM090_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case VTD6110C_GM081:
				VTD6110C_GM081_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case SD5302_GM056:
				SD5302_GM056_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case CH13A11_644:
				CH13A11_644_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case NT37701_D20:
				NT37701A_D20_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case RM692HX_C16:
				RM692HX_C16_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;			
			case RM692H5_C21:
				RM692H5_C21_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;		
			case NT37801_791:       //编号：56
				NT37801_791_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;		
			case RM692E5_M344:       //编号：57
				RM692E5_M344_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;		
			case WD6211_670:		 //0x3A-58
				WD6211_670_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
			case WD6200_644:		 //0x3B-59
				WD6200_644_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;		
			case WD6300_670_Gamma:		 //0x3C-60
				WD6300_670_Gamma_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
            case WD6300_670_Demura:		 //0x3C-60
				WD6300_670_Demura_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;            
			default:
				RM692C9_GM828_Write_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data); break;
		}
}
/********************************************************************************************
//上位机发送BGR格式，下位机转换后以RGB顺序写入补偿资料
********************************************************************************************/
void SSD2828_W_RAM_demura_buffer_part_rgb(float pclk,u8 SigMode ,u8* buffer ,u16 length,u8  multiple ,u8 DisMode)
{
  	u32 i;
	u32 Data_Num = 0;
    Data_Num = length*multiple*3;  //multiple=1
    if(SigMode == Video_Mode)
    {
        if(DisMode == 0)
        {
            for(i = 0;i<(Data_Num/2);i++)
            {
                Write_DATA(buffer[i<<1]+(buffer[(i<<1)+1]<<8));
                delay_6ns((pclk < 50) ? 50 : 0);
            } 
		}
		else if(DisMode == 1) 
        {                        
            while(Data_Num > 0)
            {
                for(i=0;i<length/2;i++)
                {
					Write_DATA(buffer[i*6+0]+(buffer[i*6+1]<<8));
					Write_DATA(buffer[i*6+2]+(buffer[i*6+3]<<8));
					Write_DATA(buffer[i*6+4]+(buffer[i*6+5]<<8));
					Data_Num-=6; 
                }                                       
            }
        } 
    }
    else if(SigMode == CMD_Mode)
    {
		#ifdef SSD_Command_16
        for(i=0;i<(Data_Num/6);i++)
        {
            Write_DATA((buffer[i*6+1]<<8)+buffer[i*6+2]);	
						delay_6ns(5);
            Write_DATA((buffer[i*6+5]<<8)+buffer[i*6+0]);	
						delay_6ns(5);
            Write_DATA((buffer[i*6+3]<<8)+buffer[i*6+4]);		
						delay_6ns(5);
        }
		#else	
        for(i=0;i<Data_Num/3;i++)
        {
            Write_DATA(buffer[i*3+2]);
            Write_DATA(buffer[i*3+1]);
            Write_DATA(buffer[i*3]);          
        }
		#endif
    }   
}

/********************************************************************************************
//上位机发送BGR格式，下位机转换后以BGR顺序写入补偿资料
********************************************************************************************/
void SSD2828_W_RAM_demura_buffer_part_bgr(float pclk,u8 SigMode ,u8* buffer ,u16 length,u8  multiple ,u8 DisMode)
{
  	u32 i;
    u32 Data_Num = 0;
    Data_Num = length*multiple*3;

	#ifdef SSD_Command_16
	for(i=0;i<(Data_Num/6);i++)
	{	
		Write_DATA((buffer[i*6+1]<<8)+buffer[i*6+0]);//210, 543
					delay_6ns(5);
		Write_DATA((buffer[i*6+5]<<8)+buffer[i*6+2]);
					delay_6ns(5);
		Write_DATA((buffer[i*6+3]<<8)+buffer[i*6+4]);								
					delay_6ns(5);	
	}
	#else	
	for(i=0;i<Data_Num/3;i++)
	{
		Write_DATA(buffer[i*3+2]);
		Write_DATA(buffer[i*3+1]);
		Write_DATA(buffer[i*3]);          
	}
	#endif 
}
void SSD2828_W_RAM_buffer_8bits_part_NT37801(float pclk,u8 SigMode ,u8* buffer ,u16 length,u8  multiple ,u8 DisMode)
{
  	u32 i;
    u32 Data_Num = 0;
    Data_Num = length*multiple*3;

	#ifdef SSD_Command_16
	for(i=0;i<(Data_Num/6);i++)
	{	
		Write_DATA((buffer[i*6+1]<<8)+buffer[i*6]);//210, 543
					delay_6ns(1);
		Write_DATA((buffer[i*6+3]<<8)+buffer[i*6+2]);
					delay_6ns(1);
		Write_DATA((buffer[i*6+5]<<8)+buffer[i*6+4]);								
					delay_6ns(1);	
	}
	#else	
	for(i=0;i<Data_Num/3;i++)
	{
		Write_DATA(buffer[i*3+2]);
		Write_DATA(buffer[i*3+1]);
		Write_DATA(buffer[i*3]);          
	}
	#endif 
}

void SSD2828_W_RAM_buffer_8bits_part_NT37701(float pclk,u8 SigMode ,u8* buffer ,u16 length,u8  multiple ,u8 DisMode)
{
  	u32 i;	
	u32 Data_Numdivided2=0;
	Data_Numdivided2=length*multiple/2;     //multiple=1
    {
		#ifdef SSD_Command_16
        for(i=0;i<(Data_Numdivided2);i++)
        {
            Write_DATA((buffer[i*2+1]<<8)+buffer[i*2+0]);
			delay_6ns(1);          
        }
		#else	
        for(i=0;i<Data_Num/3;i++)
        {
            Write_DATA(buffer[i*3+2]);
            Write_DATA(buffer[i*3+1]);
            Write_DATA(buffer[i*3]);          
        }
		#endif
    }    
}

void DemuraData2DDICRam(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{			
	if(USB_Rx_Demura==0)//第一笔数据,上位机发送数据包长度不能小于512，否则此处会产生bug
	{	
		SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (USB_Rx_Demura_Total_Num)); 
		SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((USB_Rx_Demura_Total_Num)>>16)); 											
		if(OLED.H_pixel*3<=8192)//2828 command单笔上限
		{
			SSD2828_W_Reg(OLED.SigMode,channel,0xBE,(u16) (OLED.H_pixel*3)); 									
		}			
		else
		{						
			SSD2828_W_Reg(OLED.SigMode,channel,0xBE,(u16)(OLED.H_pixel));
		}					
		SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);					
		SSD2828_W_Cmd(OLED.SigMode,channel,0x2C);
		USB_Rx_Demura=1; //第一次进入Demura数据写入状态														
	}
	//----------------------------------	
	for(i=0;i<USB_ReceivedCount;i++)
	{
		buffer[i+VCP_Receive_True_num] = USB_Rx_Buffer[i];  //
	}
	VCP_Receive_True_num+=USB_ReceivedCount;  //统计是否攒够一行
	DemuraRecivedNum+=USB_ReceivedCount;	  //统计总的接收数量	
	
	if((VCP_Receive_True_num==OLED.H_pixel*3)||(DemuraRecivedNum==USB_Rx_Demura_Total_Num)) //写入单笔数据,单次写入OLED.H_pixel*3字节
	{	
		//2832默认送3C	  
//		if(DemuraRecivedNum!=USB_Rx_Demura_Total_Num)
		{	//写入一行数据		
			SPI_CS_Select(channel,0);
			SSD2828_W_RAM_demura_buffer_part_rgb(OLED.pixel_clk,OLED.SigMode,buffer,OLED.H_pixel*3,1,0);
			SPI_CS_Select(channel,1);	
		}
//		else //(DemuraRecivedNum==USB_Rx_Demura_Total_Num)
//		{	
//			uint16_t len=0;
//			len= USB_Rx_Demura_Total_Num%(OLED.H_pixel*3);
//			//len 必须为6的倍数
//			len+=5; //多送几笔数据，凑满6的整数倍
//			SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (len)); 
//			SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((len)>>16)); 
//			SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
//			SSD2828_W_Cmd(OLED.SigMode,channel,0x3c);	
//			SPI_CS_Select(channel,0);
//			SSD2828_W_RAM_demura_buffer_part_rgb(OLED.pixel_clk,OLED.SigMode,buffer,len,1,0);  //len 必须为6的倍数
//			SPI_CS_Select(channel,1);
//		}
											
//		SPI_CS_Select(channel,0);
//		SSD2828_W_RAM_demura_buffer_part_rgb(OLED.pixel_clk,OLED.SigMode,buffer,OLED.H_pixel,1,0);
//		SPI_CS_Select(channel,1);
		
		VCP_Receive_True_num=0;
		
		if(DemuraRecivedNum==USB_Rx_Demura_Total_Num)//最后一笔，状态重置
		{
			DemuraRecivedNum=0; 	//清空 累计接受数据
			USB_Rx_Demura=0;	 	 //清空 Demura downloading状态
			Demura_START_FLAG=0;	//清空 开始Demura传输的标志位
		}
	}
}
//***************************SD5202************************************************
